package com.cupk.utils;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.AbstractClient;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.JsonResponseModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import io.lettuce.core.ShutdownArgs;

import java.time.LocalDate;


public class OCRUtil {

    public static String SECRET_ID = "AKIDVsuhGahdKmy72h7XiibK821tcBczGRpA";
    public static String SECRET_KEY = "ISbgfWEWpswvIVWN7mOo05zqW0EEzpTy";

    public static Map<String, Object> getOcrResult(String imageURL) {
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.ap-beijing.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
            req.setImageUrl(imageURL);
            // 返回的resp是一个GeneralBasicOCRResponse的实例，与请求对象对应
            GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
            // 输出json格式的字符串回包
            String jsonResponse = AbstractModel.toJsonString(resp);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> responseMap = gson.fromJson(jsonResponse, type);
            return responseMap;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public static Map<String, Object> getOcrAccurateResult(String imageURL) {
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.ap-beijing.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
            req.setImageUrl(imageURL);
            // 返回的resp是一个GeneralAccurateOCRResponse的实例，与请求对象对应
            GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
            // 输出json格式的字符串回包
            String jsonResponse = AbstractModel.toJsonString(resp);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> responseMap = gson.fromJson(jsonResponse, type);
            return responseMap;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public static Boolean judgeSpecialText(String reg, String imageURL) {
        Map<String, Object> ori = OCRUtil.getOcrResult(imageURL);
        if (ori == null) {
            return false;
        }
        List<Map<String, Object>> texts = (List<Map<String, Object>>) ori.get("TextDetections");
        String addText = "";
        for (Map<String, Object> text : texts) {
            String now = (String) text.get("DetectedText");
            Integer index = now.indexOf(reg);
            if (index != -1) {
                return true;
            }
            addText += now;
        }
        Integer index = addText.indexOf(reg);
        if (index != -1) {
            return true;
        }
        return false;
    }

    public static String judgePaymentTickets(String imageURL) {
        Map<String, Object> ori = OCRUtil.getOcrAccurateResult(imageURL);
        List<Map<String, Object>> texts = (List<Map<String, Object>>) ori.get("TextDetections");
        int currentYear = LocalDate.now().getYear();
        String currentYearStr = String.valueOf(currentYear);
        String aliPayRegex = "^" + currentYearStr + ".{28}$";
        String addText = "";
        for (Map<String, Object> text : texts) {
            String now = (String) text.get("DetectedText");
            Boolean isMatchWechat = now.matches("^1000.{28}$");
            Boolean isMatchAlipay = now.matches(aliPayRegex);
            if (isMatchWechat || isMatchAlipay) {
                return now;
            }
            if (now.matches("^[0-9]+$") && !now.equals(currentYearStr)) {
                addText += now;
            }
        }
        Boolean isMatchWechat = addText.matches("^1000.{28}$");
        Boolean isMatchAlipay = addText.matches(aliPayRegex);
        if (isMatchWechat || isMatchAlipay) {
            return addText;
        }
        return null;
    }
}
